package com.orm.analysis.dao.impl;

import com.orm.analysis.dao.GenericJPADAO;
import com.orm.analysis.model.Address;
import com.orm.analysis.model.Salesman;
import com.orm.analysis.model.Supplier;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/12/13
 * Time: 2:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class SupplierDAO extends GenericJPADAO<Supplier, Integer> {

       public List<Supplier> list(int page, int size) {
           Query query = this.em.createQuery("select a from Supplier a");
           query.setFirstResult((page - 1) * size);
           query.setMaxResults(size);
           return query.getResultList();
       }


       public Supplier getById(Integer id) {
             return this.em.find(Supplier.class,id);
       }


    @Transactional
    @Override
    public void save(Supplier entity) {
        getEntityManager().persist(entity);
    }

    @Transactional
                @Override
                    public void delete(Object id) {
                        getEntityManager().remove(getEntityManager().find(Supplier.class, id));

                     }

    public List<Supplier> findByIdList(Integer[] entityIdList){

            Query query = this.em.createQuery("select a from Supplier a  where a.id in (:entityIdList)");
            query.setParameter("entityIdList", Arrays.asList(entityIdList));
            return query.getResultList();
        }



        public List<Integer> findIdList() {
            Query query = this.em.createQuery("select a.id from Supplier a ");
            return query.getResultList();
        }


}
